﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling


Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections


Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio


Imports visRackComponentAttributes.dc_PortBase
Imports visRackComponentAttributes.dc_Port
Imports visRackComponentAttributes.clsDC_RackData
Imports visRackComponentAttributes.dc_Component



Public Class clsAttrPortData


#Region " Properties and Definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_Port
    Public m_oc As Collection(Of dc_Port)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand


    Public _clsVisio As visRackComponentAttributes.clsVisioSupport

    Public Const readAll As String = "selectTblAttrPortData"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrPortData

    Private m_visshape As Microsoft.Office.Interop.Visio.Shape
    Public Property _VisShape() As Microsoft.Office.Interop.Visio.Shape
        Get
            Return Me.m_visshape
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Shape)
            m_visshape = value
        End Set
    End Property

    '********* visio snippet vb.net vis0041vb ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net vis0043vb ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property

    '********* visio snippet vb.net vis0045vb ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net vis0044vb ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property

    Public m_customstencilpath As String = My.Settings.path_custom_stencils
    Public Property _CustomStencilPath() As String
        Get
            Return Me.m_customstencilpath
        End Get
        Set(ByVal value As String)
            Me.m_customstencilpath = value
        End Set
    End Property


#End Region

#Region " Move/Transform Data "



    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Port = New dc_Port(strUserId)

        Try
            Dim colportKey As New System.Data.DataColumn
            colportKey.ColumnName = "portKey"
            colportKey.DataType = GetType(Integer)
            colportKey.DefaultValue = m_dc.portKey
            tempTable.Columns.Add(colportKey)

            Dim colportCompKey As New System.Data.DataColumn
            colportCompKey.ColumnName = "portCompKey"
            colportCompKey.DataType = GetType(Integer)
            colportCompKey.DefaultValue = m_dc.portCompKey
            tempTable.Columns.Add(colportCompKey)

            Dim colportProductKey As New System.Data.DataColumn
            colportProductKey.ColumnName = "portProductKey"
            colportProductKey.DataType = GetType(Integer)
            colportProductKey.DefaultValue = m_dc.portProductKey
            tempTable.Columns.Add(colportProductKey)

            Dim colportName As New System.Data.DataColumn
            colportName.ColumnName = "portName"
            colportName.DataType = GetType(String)
            colportName.DefaultValue = m_dc.portName
            tempTable.Columns.Add(colportName)

            Dim colportProductView As New System.Data.DataColumn
            colportProductView.ColumnName = "portProductView"
            colportProductView.DataType = GetType(dc_Product.View)
            colportProductView.DefaultValue = m_dc.portProductView
            tempTable.Columns.Add(colportProductView)

            Dim colportTechnology As New System.Data.DataColumn
            colportTechnology.ColumnName = "portTechnology"
            colportTechnology.DataType = GetType(Integer)
            colportTechnology.DefaultValue = m_dc.portTechnology
            tempTable.Columns.Add(colportTechnology)

            Dim colportConnector As New System.Data.DataColumn
            colportConnector.ColumnName = "portConnector"
            colportConnector.DataType = GetType(Integer)
            colportConnector.DefaultValue = m_dc.portConnector
            tempTable.Columns.Add(colportConnector)

            Dim colportSpeed As New System.Data.DataColumn
            colportSpeed.ColumnName = "portSpeed"
            colportSpeed.DataType = GetType(Integer)
            colportSpeed.DefaultValue = m_dc.portSpeed
            tempTable.Columns.Add(colportSpeed)

            Dim colportStatus As New System.Data.DataColumn
            colportStatus.ColumnName = "portStatus"
            colportStatus.DataType = GetType(enumPortStatus)
            colportStatus.DefaultValue = m_dc.portStatus
            tempTable.Columns.Add(colportStatus)

            Dim colportDuplex As New System.Data.DataColumn
            colportDuplex.ColumnName = "portDuplex"
            colportDuplex.DataType = GetType(enumPortDuplex)
            colportDuplex.DefaultValue = m_dc.portDuplex
            tempTable.Columns.Add(colportDuplex)

            Dim colportUtilForward As New System.Data.DataColumn
            colportUtilForward.ColumnName = "portUtilForward"
            colportUtilForward.DataType = GetType(Double)
            colportUtilForward.DefaultValue = m_dc.portUtilForward
            tempTable.Columns.Add(colportUtilForward)

            Dim colportUtilReverse As New System.Data.DataColumn
            colportUtilReverse.ColumnName = "portUtilReverse"
            colportUtilReverse.DataType = GetType(Double)
            colportUtilReverse.DefaultValue = m_dc.portUtilReverse
            tempTable.Columns.Add(colportUtilReverse)

            Dim colportComments As New System.Data.DataColumn
            colportComments.ColumnName = "portComments"
            colportComments.DataType = GetType(String)
            colportComments.DefaultValue = m_dc.portComments
            tempTable.Columns.Add(colportComments)

            Dim colportLastUpdate As New System.Data.DataColumn
            colportLastUpdate.ColumnName = "portLastUpdate"
            colportLastUpdate.DataType = GetType(Date)
            colportLastUpdate.DefaultValue = m_dc.portLastUpdate
            tempTable.Columns.Add(colportLastUpdate)

            Dim colportLastEdit As New System.Data.DataColumn
            colportLastEdit.ColumnName = "portLastEdit"
            colportLastEdit.DataType = GetType(String)
            colportLastEdit.DefaultValue = m_dc.portLastEdit
            tempTable.Columns.Add(colportLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub

    '
    ' use this initially for adding rows
    '
    Public Sub movePropertiesToDataRow _
                (ByRef tempRow As DataRow, _
                 ByVal tempDC As dc_Port)

        Try
            With tempDC
                tempRow.SetField("portKey", .portKey)
                tempRow.SetField("portCompKey", .portCompKey)
                tempRow.SetField("portProductKey", .portProductKey)
                tempRow.SetField("portName", .portName)
                tempRow.SetField("portProductView", .portProductView)
                tempRow.SetField("portTechnology", .portTechnology)
                tempRow.SetField("portConnector", .portConnector)
                tempRow.SetField("portSpeed", .portSpeed)
                tempRow.SetField("portStatus", .portStatus)
                tempRow.SetField("portDuplex", .portDuplex)
                tempRow.SetField("portUtilForward", .portUtilForward)
                tempRow.SetField("portUtilReverse", .portUtilReverse)
                tempRow.SetField("portComments", .portComments)
                tempRow.SetField("portLastUpdate", .portLastUpdate)
                tempRow.SetField("portLastEdit", strUserId)
            End With

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .portKey = tempRow.Item("portKey")
                .portCompKey = tempRow.Item("portCompKey")
                .portProductKey = tempRow.Item("portProductKey")
                .portName = tempRow.Item("portName")
                .portProductView = tempRow.Item("portProductView")
                .portTechnology = tempRow.Item("portTechnology")
                .portConnector = tempRow.Item("portConnector")
                .portSpeed = tempRow.Item("portSpeed")
                .portStatus = tempRow.Item("portStatus")
                .portDuplex = tempRow.Item("portDuplex")
                .portUtilForward = tempRow.Item("portUtilForward")
                .portUtilReverse = tempRow.Item("portUtilReverse")
                .portComments = tempRow.Item("portComments")
                .portLastUpdate = tempRow.Item("portLastUpdate")
                .portLastEdit = tempRow.Item("portLastEdit")
            End With

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow, _
                 ByRef tempDC As dc_Port)

        Try

            With tempDC
                .initProperties()
                .portKey = tempRow.Item("portKey")
                .portCompKey = tempRow.Item("portCompKey")
                .portProductKey = tempRow.Item("portProductKey")
                .portName = tempRow.Item("portName")
                .portProductView = tempRow.Item("portProductView")
                .portTechnology = tempRow.Item("portTechnology")
                .portConnector = tempRow.Item("portConnector")
                .portSpeed = tempRow.Item("portSpeed")
                .portStatus = tempRow.Item("portStatus")
                .portDuplex = tempRow.Item("portDuplex")
                .portUtilForward = tempRow.Item("portUtilForward")
                .portUtilReverse = tempRow.Item("portUtilReverse")
                .portComments = tempRow.Item("portComments")
                .portLastUpdate = tempRow.Item("portLastUpdate")
                .portLastEdit = tempRow.Item("portLastEdit")
            End With

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Port)
                Else
                    m_oc = New Collection(Of dc_Port)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Port
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Port
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function


#End Region

#Region " standard calls "

    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Port)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Port)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_Port(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Port(strUserId)
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblattrportdata.tostring
            m_ds.Tables.Add(m_dt)



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addAttrPortDataSourceRecord _
        (ByVal tempDC As dc_Port, _
         Optional ByVal blnSilent As Boolean = False) _
        As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblattrportdata.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(DC_RackTables.tblAttrPortData.ToString).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()


            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    '
    '
    Private Sub buildUpdatePortDataCommand()

        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateAttrPortData")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand
            m_db.AddInParameter(cmdActive, "portCompKey", DbType.Int32, "portCompKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portProductKey", DbType.Int32, "portProductKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portName", DbType.String, "portName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portProductView", DbType.String, "portProductView", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portTechnology", DbType.Int16, "portTechnology", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portConnector", DbType.Int16, "portConnector", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portSpeed", DbType.Int32, "portSpeed", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portStatus", DbType.Int16, "portStatus", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portDuplex", DbType.Int16, "portDuplex", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portUtilForward", DbType.Single, "portUtilForward", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portUtilReverse", DbType.Single, "portUtilReverse", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portComments", DbType.String, "portComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portLastUpdate", DbType.Date, "portLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portLastEdit", DbType.String, "portLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildInsertPortDataCommand()

        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertAttrPortData")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand
            m_db.AddInParameter(cmdActive, "portCompKey", DbType.Int32, "portCompKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portProductKey", DbType.Int32, "portProductKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portName", DbType.String, "portName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portProductView", DbType.String, "portProductView", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portTechnology", DbType.Int16, "portTechnology", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portConnector", DbType.Int16, "portConnector", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portSpeed", DbType.Int32, "portSpeed", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portStatus", DbType.Int16, "portStatus", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portDuplex", DbType.Int16, "portDuplex", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portUtilForward", DbType.Single, "portUtilForward", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portUtilReverse", DbType.Single, "portUtilReverse", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portComments", DbType.String, "portComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portLastUpdate", DbType.Date, "portLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "portLastEdit", DbType.String, "portLastEdit", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeletePortDataCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrPortDataByPortKey")
            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand
            m_db.AddInParameter(cmdActive, "portKey", DbType.Int32, "portKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' take the existing portdataTable and put it back into the database
    ' with any changes that may have occured
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblattrportdata.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function



#End Region

#Region " database calls "

    '
    '
    '
    Public Sub dbSelectAllPortData()


        Try

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_Port
            Else
                m_dc = New dc_Port
            End If

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrportdata.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblattrportdata.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectAttrPortDataByCompKey(ByVal intCompKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrPortDataByCompKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblAttrPortData.ToString)

            m_dt = m_ds.Tables(dc_racktables.tblattrportdata.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' need a command so when the component goes away we can clean up the port data
    '
    Private Sub dbDeleteAttrPortDataByCompKey()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrPortDataByCompKey")
            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand
            m_db.AddInParameter(cmdActive, "portCompKey", DbType.Int32, "portCompKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectAttrPortDataByPortKey(ByVal intPortKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrPortDataByPortKey")
            m_db.AddInParameter(cmd, "@portKey", DbType.Int32, intPortKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrportdata.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblattrportdata.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' we use this one to chase a collection of ports, usually for the connection data class
    '
    Public Sub dbSelectAttrPortDataByPortKeyCollection(ByVal colPortKeys As Collection)


        Dim dtTemp As DataTable = New DataTable
        Dim dtTempIn As DataTable = New DataTable
        Dim drTemp As DataRow
        Dim intPortKey As Int32


        Try


            ' use the collection of portkeys to create a custom portkey table
            For intCt = 1 To colPortKeys.Count
                If dtTempIn Is Nothing Then
                    dtTempIn = New DataTable
                Else
                    dtTempIn = Nothing
                    dtTempIn = New DataTable
                End If
                intPortKey = colPortKeys.Item(intCt)
                dbSelectAttrPortDataByPortKey(intPortKey)
                dtTempIn = m_dt.Copy
                If intCt = 1 Then
                    dtTemp = dtTempIn.Copy
                Else
                    For Each drTemp In dtTempIn.Rows
                        With drTemp
                            dtTemp.ImportRow(drTemp)
                        End With
                    Next
                End If
            Next intCt ' next port key

            ' clean the data caches again
            initDataSetAndTable()

            dtTemp.TableName = DC_RackTables.tblAttrPortData.ToString
            m_dt = dtTemp.Copy
            If m_ds.Tables(myEnum.ToString) IsNot Nothing Then
                m_ds.Tables(myEnum.ToString).Clear()
                For Each drTemp In dtTemp.Rows
                    m_ds.Tables(myEnum.ToString).ImportRow(drTemp)
                Next
            Else
                m_ds.Tables.Add(dtTemp.Copy)
            End If


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub



    '
    ' Go through the PortData table and ensure that the productKey represents the
    ' component - needed so that if we want to display the component/port 
    '
    Public Sub dbUpdateAttrPortDataProductKey()

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("updateComponentNetworkStatus")

            Dim affectedRows As Int16 = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Public Sub dbSelectAttrPortDataByRowKey(ByVal intRowKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrPortDataByRowKey")
            m_db.AddInParameter(cmd, "@rowKey", DbType.Int32, intRowKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrportdata.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblattrportdata.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    ' the table returned is named "temp" and is intended for the composite row query
    '
    Public Function dbSelectAttrPortDataByRack _
                (ByVal lngRackKey As Int32) _
                As DataTable

        Dim m_dt1 As DataTable = New DataTable
        Dim dtReturn As DataTable = New DataTable
        Dim m_ds1 As DataSet = New DataSet

        Try

            Dim cmd1 As DbCommand
            cmd1 = m_db.GetStoredProcCommand("selectAttrPortDataByRackKey")
            m_db.AddInParameter(cmd1, "@compKey", DbType.Int32, lngRackKey)
            m_db.LoadDataSet(cmd1, m_ds1, "temp")
            m_dt1 = m_ds1.Tables("temp")

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
            m_dt1 = Nothing
        End Try

        dbSelectAttrPortDataByRack = m_dt1

    End Function

    '
    '
    '
    Public Sub dbSelectAttrPortDataByCompositeRow _
        (ByVal colURRIKeys As Collection(Of clsDC_RackData.rackURRI))

        initDataSetAndTable()


        Dim dtInput As DataTable = New DataTable
        Dim dtOutput As DataTable = New DataTable
        Dim drTemp As DataRow
        Dim lngRackKey As Int32
        Dim colRacks As Collection = New Collection
        Dim rckUrri As clsDC_RackData.rackURRI

        For Each rckURRI In colURRIKeys
            colRacks.Add(rckUrri.rckKey)
        Next rckURRI

        Try

            For intX = 1 To colRacks.Count
                lngRackKey = colRacks(intX)
                If Not (dtInput Is Nothing) Then
                    dtInput = Nothing
                    dtInput = New DataTable
                Else
                    dtInput = New DataTable
                End If
                dtInput = dbSelectAttrPortDataByRack(lngRackKey)
                With dtInput
                    If intX = 1 Then
                        dtOutput = Nothing
                        dtOutput = New DataTable
                        dtOutput = dtInput.Copy
                    Else
                        For Each drTemp In .Rows
                            dtOutput.ImportRow(drTemp)
                        Next
                    End If
                End With
            Next intX


            For Each drTemp In dtOutput.Rows
                m_ds.Tables(dc_racktables.tblattrportdata.tostring).ImportRow(drTemp)
            Next

            m_dt = m_ds.Tables(DC_RackTables.tblAttrPortData.ToString)
            m_dt.TableName = DC_RackTables.tblAttrPortData.ToString

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



#End Region

#Region " methods "




#End Region

#Region " Constructor "

    '
    '
    '
    Public Sub New(ByVal visApp As Microsoft.Office.Interop.Visio.Application, _
                   ByVal visDoc As Microsoft.Office.Interop.Visio.Document)
        Try


            m_db = DatabaseFactory.CreateDatabase()
            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '
            If m_dc Is Nothing Then
                m_dc = New dc_Port(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Port(strUserId)
            End If

            initDataSetAndTable()


            _clsVisio = New visRackComponentAttributes.clsVisioSupport
            _clsVisio.drawingApplication = visApp
            _clsVisio.drawingDocument = visDoc

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertPortDataCommand()
            buildDeletePortDataCommand()
            buildUpdatePortDataCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


#End Region

#Region " Visio Properties "

    '
    '
    '
    Public Sub UpdatePortProperties _
        (ByRef visShape As Microsoft.Office.Interop.Visio.Shape, _
         ByVal drPort As System.Data.DataRow)

        Try


            moveDataRowToProperties(drPort)

            With m_dc

                _clsVisio.ForceShapeData(visShape, "portKey", CStr(.portKey), "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portCompKey", CStr(.portCompKey), "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portProductKey", CStr(.portProductKey), "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portName", .portName, "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portProductView", CStr(.portProductView), "attrPortData")
                Dim intPortTech As dc_PortBase.PortTech = .portTechnology
                _clsVisio.ForceShapeData(visShape, "portTechnology", intPortTech.ToString, "attrPortData")
                Dim intPortConn As dc_PortBase.PortConn = .portConnector
                _clsVisio.ForceShapeData(visShape, "portConnector", intPortConn.ToString, "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portSpeed", CStr(.portSpeed), "attrPortData")
                Dim intPortStatus As enumPortStatus = .portStatus
                _clsVisio.ForceShapeData(visShape, "portStatus", intPortStatus.ToString, "attrPortData")
                Dim intPortDuplex As enumPortDuplex = .portDuplex
                _clsVisio.ForceShapeData(visShape, "portDuplex", intPortDuplex.ToString, "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portUtilForward", CStr(.portUtilForward), "attrPortData")
                _clsVisio.ForceShapeData(visShape, "portUtilReverse", CStr(.portUtilReverse), "attrPortData")

            End With


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    Private Const constPortPinX As String = "6.5 in"
    Private Const constPortPinY As String = "8 in"
    Private Const constPortAlign As VisListAlignment = VisListAlignment.visListAlignLeftOrTop
    Private Const constPortDir As VisListDirection = VisListDirection.visListDirTopToBottom
    Private Const constPortHeader As String = "Ports"
    Private Const constPortMaster As String = "Container 4"

    ' wireframe stencil for list (vertical w/header on side and horizontal) toolbar 
    Const stencilWFToolbar As String = "wftlbr_u.vss"
    Const masterDropDown As String = "Drop-down menu"
    Const masterDropDownItem As String = "Menu item"

    '
    ' finds all of the ports for a given component and puts them into a container
    ' this build container needed to be extended because when pulling ports for a component such as a 
    ' network patch panel we could end up with 60+ items
    '
    Public Function buildPortDataContainer _
                        (ByVal strSource As String, _
                         ByVal intKey As Int32, _
                         ByVal _clsData As clsDC_RackData, _
                         Optional ByVal blnUseItemSurrogate As Boolean = False) _
                        As Microsoft.Office.Interop.Visio.Shape

        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing
        Dim strStencil As String = ""
        Dim strMaster As String = ""
        Dim strDGMaster As String = ""
        Dim visDG As Microsoft.Office.Interop.Visio.Master = Nothing
        Dim intLabelFont As Integer
        Dim intValueFont As Integer
        Dim dblCalloutWidth As Double

        ' just to make sure
        _clsVisio.drawingPage = drawingPage
        drawingDocument = drawingPage.Document

        Try


            Dim colInput As Collection = New Collection
            Dim drTemp As System.Data.DataRow = Nothing
            Dim portQuery As System.Data.EnumerableRowCollection(Of System.Data.DataRow)


            If strSource = "comp" Then

                ' find our component
                portQuery = From compport In _clsData.m_ds.Tables(DC_RackTables.tblAttrPortData.ToString).AsEnumerable() _
                           Where compport!portcompkey = intKey
                          Select compport

            Else

                ' find our port record
                portQuery = From linkport In _clsData.m_ds.Tables(DC_RackTables.tblAttrPortData.ToString).AsEnumerable() _
                    Where linkport!portkey = intKey
                    Select linkport

            End If


            If portQuery.Count = 0 Then Return shpContainer


            For Each p In portQuery
                colInput.Add("portKey_" & p.Item("portKey").ToString)
                'System.Diagnostics.Debug.WriteLine(p.Item("portName").ToString)
                'System.Diagnostics.Debug.WriteLine(p.Item("portKey").ToString)
            Next p

            ' create a list control
            Dim listControl As Microsoft.Office.Interop.Visio.Shape = Nothing
            ' pass in the created collection and return a populated list control
            Dim colMembers As Collection = New Collection

            If blnUseItemSurrogate = vbFalse Then

                colMembers = _clsVisio.dropDropDownBoxItems(colInput, masterDropDownItem, stencilWFToolbar, "1.5in", ".25in", vbTrue)

            Else


                Dim dgmasterQuery = From datagraphic In _clsData.m_ds.Tables(DC_RackTables.tblDGMasters.ToString).AsEnumerable() _
                                    Where datagraphic!dgmTable = DC_RackTables.tblAttrPortData.ToString _
                                    Select datagraphic

                ' should only be one..
                For Each m In dgmasterQuery
                    If LCase(m!dgmTable) = LCase(DC_RackTables.tblAttrPortData.ToString) Then
                        strStencil = m!dgmSurrogateStencil
                        strStencil = _CustomStencilPath & strStencil
                        strMaster = m!dgmSurrogateMaster
                        intLabelFont = CInt(m!dgmFontLabel)
                        intValueFont = CInt(m!dgmFontValue)
                        dblCalloutWidth = CDbl(m!dgmCalloutWidth)
                        strDGMaster = m!dgmMaster
                        Exit For
                    End If
                Next

                ' pass in the created collection and return a populated list control
                colMembers = _clsVisio.dropDropDownBoxItems(colInput, strMaster, strStencil, "1.5", "1.0", vbTrue)


            End If


            Dim visShape As Microsoft.Office.Interop.Visio.Shape = Nothing
            Dim visCell As Microsoft.Office.Interop.Visio.Cell = Nothing
            Dim strName As String = ""
            Dim strKey As String = ""

            For Each shapeId In colMembers
                ' get the shape
                visShape = drawingPage.Shapes.ItemFromID(shapeId)
                If visShape.CellExists("user.attrkey", False) = True Then
                    strName = visShape.Cells("user.attrkey").ResultStr("")
                Else
                    strName = ""
                    GoTo NextShape
                End If

                ' clear any shape data properties
                _clsVisio.deleteShapeData(visShape)

                ' set it semi-transparent
                Dim strTrans As String = "50%"
                ' first the fill properties
                _clsVisio.recurseApplyCellFormula(visShape, VisSectionIndices.visSectionObject, VisRowIndices.visRowFill, VisCellIndices.visFillForegndTrans, strTrans)
                _clsVisio.recurseApplyCellFormula(visShape, VisSectionIndices.visSectionObject, VisRowIndices.visRowFill, VisCellIndices.visFillBkgndTrans, strTrans)
                ' then the line properties
                _clsVisio.recurseApplyCellFormula(visShape, VisSectionIndices.visSectionObject, VisRowIndices.visRowLine, VisCellIndices.visLineColorTrans, strTrans)
                ' prep it for applying the datagraphics by applying the data from the dgmasters table
                _clsVisio.addDataGraphicUserFieldsToShape _
                        (visShape, _
                        intValueFont.ToString, _
                        intLabelFont.ToString, _
                        dblCalloutWidth.ToString)

                'Check that the requested Data Graphic Master exits
                For Each visMaster In drawingDocument.Masters
                    If visMaster.Type = VisMasterTypes.visTypeDataGraphic Then
                        If visMaster.Name = strDGMaster Then
                            visDG = visMaster
                            Exit For
                        End If
                    End If
                Next

                ' go read the current records
                For Each p In portQuery
                    strKey = "portKey_" & p.Item("portKey").ToString
                    If strKey = strName Then

                        UpdatePortProperties(visShape, p)
                        visShape.Text = p.Item("portName").ToString

                        ' allow the legend to keep track of the shapes
                        _clsVisio.ForceUserProperty(visShape, "visLegendShape", "2.0")

                        ' move the name to comment field so we get mouse over
                        If visShape.CellsSRCExists(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment, False) = True Then
                            visCell = visShape.CellsSRC(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment)
                            _clsVisio.SetCellValueToString(visCell, strName)
                        End If

                        ' if it is using a surrogate then attempt to apply the datagraphic
                        If blnUseItemSurrogate = True Then

                            'Apply the Data Graphic to the Shape
                            visShape.DataGraphic = visDG

                            ' unlock the groups so we can change the font sizes
                            _clsVisio.recurseApplyCellFormula(visShape, VisSectionIndices.visSectionObject, VisRowIndices.visRowLock, VisCellIndices.visLockGroup, "0")

                            If visShape.CellExists("user.dbIntLabelFont", False) = True Then

                                ' get the loaded values 
                                intLabelFont = CInt(visShape.Cells("user.dbintlabelfont").ResultIU)
                                intValueFont = CInt(visShape.Cells("user.dbintvaluefont").ResultIU)
                                dblCalloutWidth = CDbl(visShape.Cells("user.dbdblcallout").ResultIU)

                                ' set the callout width = 85% of the surrogate shapes width (as a formula) so that it fits inside the shape
                                Dim strId As String = visShape.NameID & "!Width*.85"
                                strId = "Guard(" & strId & ")"

                                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutproplabelsize", intLabelFont.ToString & " pt")
                                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutpropvaluesize", intValueFont.ToString & " pt")
                                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutpropwidth", strId)

                            End If

                        End If

                        Exit For
                    End If
                Next p
NextShape:
            Next shapeId

            ' add the container to hold the list
            shpContainer = _clsVisio.addContainerToPage(constPortMaster, "", "")

            listControl = _clsVisio.loadListControl(colMembers, constPortAlign, constPortDir)
            'listControl.Name = "listPorts"
            'listControl.Name = "listPorts"

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainer, listControl)

            shpContainer.Cells("pinx").Formula = constPortPinX
            shpContainer.Cells("piny").Formula = constPortPinY


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


        System.Windows.Forms.Application.DoEvents()
        Return shpContainer

    End Function



#End Region



End Class
